<?php

namespace app\common\job;

use ShidianUtil\Api\Toutiao\Douyin\Douyin;
use think\facade\Config;
use think\queue\Job;

/**
 * 抖音小程序订阅消息
 * Class DouyinMiniProgramMessage
 * @package app\common\job
 */
class DouyinMiniProgramMessage
{
    /**
     * fire是消息队列默认调用的方法
     * @param Job $job 当前的任务对象
     * @param array|mixed $data 发布任务时自定义的数据
     */
    public function fire(Job $job, $data)
    {

        $appid = $data['appid'] ?? Config::get('douyin.' . DOUYIN_MINIPROGRAM . '.appid', '');
        $openid = $data['openid'] ?? '';
        $tpl_id = $data['tpl_id'] ?? '';
        $template_data = $data['data'] ?? [];
        $page = $data['page'] ?? '';

        $this->getDouyin()->miniProgram(DOUYIN_MINIPROGRAM)
            ->subscribeMessage()
            ->send($appid, $openid, $tpl_id, $template_data, $page);

        $job->delete();
    }

    private function getDouyin(): Douyin
    {
        return app(Douyin::class);
    }
}
